package cn.xxm.dongbao.user.secure.dos;

import javax.servlet.http.HttpServletRequest;

/**
 * Request statistics per session is saved into repository. Usually the request statistics object is saved into session
 * {@link HttpSessionDosStatisticsRepository} Sometimes it is distribute cluster, session content can't be consistency.
 * Implement the DosStatisticsRepository{@link DosStatisticsRepository} to make the distribute cache repository.
 */
public interface DosStatisticsRepository {
    /**
     * Get the DosStatistics{@link TokenBucket} object from repository.
     * @param request HttpServletRequest
     * @param config Dos Config
     * @return Token bucket
     * @throws DosStatisticsException Dos statistics exception
     */
    TokenBucket getStatistics(HttpServletRequest request, DosConfig config) throws DosStatisticsException;

    /**
     * Remove the DosStatistics{@link TokenBucket} from repository
     * @param request HttpServletRequest
     * @param bucket Token bucket
     * @throws DosStatisticsException Dos statistics exception
     */
    void removeStatistics(HttpServletRequest request, TokenBucket bucket) throws DosStatisticsException;

    /**
     * Save the DosStatistics{@link TokenBucket} object into repository.
     * @param request HttpServletRequest
     * @param bucket Token bucket
     * @throws DosStatisticsException Dos statistics exception
     */
    void saveStatistics(HttpServletRequest request, TokenBucket bucket) throws DosStatisticsException;
}